home *** CD-ROM | disk | FTP | other *** search
/ System Booster / System Booster.iso / Archives / ForCLI / PrintDate.lha / PrintDate / printdate.c < prev    next >
Encoding:
C/C++ Source or Header  |  1992-02-02  |  4.6 KB  |  197 lines

  1. /* $Revision Header *** Header built automatically - do not edit! ***********
  2.  *
  3.  *    (C) Copyright 1991 by Torsten Jürgeleit
  4.  *
  5.  *    Name .....: printdate.c
  6.  *    Created ..: Thursday 19-Dec-91 14:00:33
  7.  *    Revision .: 0
  8.  *
  9.  *    Date        Author                 Comment
  10.  *    =========   ====================   ====================
  11.  *    19-Dec-91   Torsten Jürgeleit      Created this file!
  12.  *
  13.  ***************************************************************************
  14.  *
  15.  *    This is a very small subset of UDate to produce a free formatable
  16.  *    output string of the current date and time.
  17.  *
  18.  *
  19.  * Format commands:
  20.  *
  21.  *    \a = day
  22.  *    \d = date (dd-mmm-yy)
  23.  *    \t = time
  24.  *    \q = double quotes
  25.  *    \e = escape (0x1b = 27)
  26.  *    \n = line feed
  27.  *    \\ = backslash
  28.  *
  29.  *
  30.  * Example:
  31.  *
  32.  *    PrintDate FORMAT "*tdc.b*t\q (\a \d \t)\q,13,10,0"
  33.  *        ->        dc.b    "Monday 11-Nov-91 11:11:11",13,10,0
  34.  *
  35.  *
  36.  * Default format:
  37.  *
  38.  *    Same as AmigaDOS Date command ("\a \d \t").
  39.  *
  40.  *
  41.  * Caveats:
  42.  *
  43.  *    Do NOT use '\' as escape character for the ARP library too. To check
  44.  *    this use the ARP command 'set listall'.    (Default: set escape *)
  45.  *    Otherwise the ARP command line parsing function GADS would interpret
  46.  *    the format commands as escape sequences.
  47.  *
  48.  * $Revision Header ********************************************************/
  49.  
  50.     /* Includes */
  51.  
  52. #include <exec/types.h>
  53. #include <libraries/arpbase.h>
  54. #include <functions.h>
  55.  
  56.     /* Defines */
  57.  
  58. #define MAX_ARGUMENTS        2
  59.  
  60. #define ARGUMENT_NOLINE        0
  61. #define ARGUMENT_FORMAT        1
  62.  
  63. #define DEFAULT_FORMAT        "\\a \\d \\t"
  64.  
  65.     /* Externals */
  66.  
  67. IMPORT struct DOSBase  *DOSBase;
  68.  
  69.     /* Globals */
  70.  
  71. struct ArpBase    *ArpBase;
  72.  
  73. BYTE *template  = "NOLINE/S,FORMAT/K",
  74.      *xtra_help = "Usage: PrintDate [NOLINE] [FORMAT <format>]\n"
  75.           "\t[NOLINE] = suppress line feed\n"
  76.           "\t<format> = alternate output format string, "
  77.                 "default is \x22" DEFAULT_FORMAT "\x22";
  78.     /* Main routine - no startup code */
  79.  
  80.    LONG
  81. _main(LONG alen, BYTE *aptr)
  82. {
  83.    struct DateTime  date_time, *dat = &date_time;
  84.    BYTE  str_day[LEN_DATSTRING], str_date[LEN_DATSTRING],
  85.      str_time[LEN_DATSTRING], *argv[MAX_ARGUMENTS];
  86.    BYTE  c, *format;
  87.    SHORT i;
  88.    BOOL  error = FALSE;
  89.    LONG  return_code = RETURN_FAIL;
  90.  
  91.    /* Open ARP library */
  92.    if (!(ArpBase = OpenLibrary(ArpName, ArpVersion))) {
  93.       Write(Output(), "Can't open ARP library V39+\n", 27L);
  94.    } else {
  95.  
  96.       /* Get actual date and time into DateStamp and do range check */
  97.       DateStamp(&dat->dat_Stamp);
  98.       if (dat->dat_Stamp.ds_Days > 36500 || dat->dat_Stamp.ds_Minute >
  99.                   60 * 24 || dat->dat_Stamp.ds_Tick > 50 * 60) {
  100.      Puts("Invalid date stamp received from AmigaDOS");
  101.       } else {
  102.  
  103.      /* Initialize DateTime structure */
  104.      dat->dat_Format  = FORMAT_DOS;
  105.      dat->dat_Flags   = 0;
  106.      dat->dat_StrDay  = str_day;
  107.      dat->dat_StrDate = str_date;
  108.      dat->dat_StrTime = str_time;
  109.  
  110.      /* NULL terminate time string */
  111.      str_time[8] = '\0';
  112.  
  113.      /* Convert DateStamp to strings in DateTime */
  114.      if (StamptoStr(dat)) {
  115.         Puts("StamptoStr() failed");
  116.      } else {
  117.  
  118.         /* Clear argument array */
  119.         for (i = 0; i < MAX_ARGUMENTS; i++) {
  120.            argv[i] = NULL;
  121.         }
  122.  
  123.         /* Parse command line arguments */
  124.         if (GADS(aptr, alen, xtra_help, &argv[0], template) < 0) {
  125.            Puts(argv[0]);
  126.         } else {
  127.  
  128.            /* If no format string given then use default format */
  129.            if (!(format = argv[ARGUMENT_FORMAT])) {
  130.           format = DEFAULT_FORMAT;
  131.            }
  132.  
  133.            /* Build date string from given format */
  134.            while (error == FALSE && (c = *format++) != '\0') {
  135.  
  136.           /* Escape character ? */
  137.           if (c == '\\') {
  138.  
  139.              /* Insert special date */
  140.              switch (c = *format++) {
  141.             case 'a' :   /* Day */
  142.                Printf("%s", dat->dat_StrDay);
  143.                break;
  144.  
  145.             case 'd' :   /* Date */
  146.                Printf("%s", dat->dat_StrDate);
  147.                break;
  148.  
  149.             case 't' :   /* Time */
  150.                Printf("%s", dat->dat_StrTime);
  151.                break;
  152.  
  153.             case 'q' :   /* Double quotes */
  154.                Printf("\"");
  155.                break;
  156.  
  157.             case 'e' :   /* Escape */
  158.                Printf("\x1b");
  159.                break;
  160.  
  161.             case 'n' :   /* New line */
  162.                Printf("\n");
  163.                break;
  164.  
  165.             case '\\' :   /* Backslash */
  166.                Printf("\\");
  167.                break;
  168.  
  169.             default :    /* Invalid format type */
  170.                Printf("\nInvalid format type '\%c'\n", c);
  171.                error = TRUE;
  172.                break;
  173.              
  174.              }
  175.           } else {
  176.              Printf("%c", c);
  177.           }
  178.            }
  179.  
  180.            /* If no error then append line feed */
  181.            if (error == FALSE) {
  182.           if (!argv[ARGUMENT_NOLINE]) {
  183.              Printf("\n");
  184.           }
  185.            }
  186.            return_code = RETURN_OK;
  187.         }
  188.      }
  189.       }
  190.       CloseLibrary(ArpBase);
  191.    }
  192.  
  193.    /* MANX crt0.asm forget to close DOS library, so we have to do it */
  194.    CloseLibrary(DOSBase);
  195.    return(return_code);
  196. }
  197.